package com.testingedu.action.front;

import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;

import javax.servlet.ServletOutputStream;

import com.testingedu.action.background.RequestContext;

public class TFileDownloadAction extends RequestContext{

	private static final long serialVersionUID = 4098219294828154876L;
	
	public void download_() throws Exception {
		//处理请求  
        //读取要下载的文件  
        File f = new File("G:\\java\\apache-tomcat-7.0.26\\webapps\\testingedu2\\WEB-INF\\resources\\51CTO下载-J2EE系统架构师参考手册.doc");  
        if(f.exists()){  
        	System.out.println("yes");
            FileInputStream  fis = new FileInputStream(f);  
            String filename=URLEncoder.encode(f.getName(),"utf-8"); //解决中文文件名下载后乱码的问题  
            byte[] b = new byte[fis.available()];  
            fis.read(b);  
            response.setCharacterEncoding("utf-8");  
            response.setHeader("Content-Disposition","attachment; filename="+filename+"");  
            //获取响应报文输出流对象  
            ServletOutputStream  out =response.getOutputStream();  
            //输出  
            out.write(b);  
            out.flush();  
            out.close();  
        }     
	}
}
